package cn.dansj.common.utils.transfer;

import cn.dansj.common.utils.enums.GlobalEnv;
import cn.dansj.common.utils.json.Feature;
import cn.dansj.common.utils.json.JSONUtils;
import cn.dansj.common.utils.json.JSONObject;
import com.fasterxml.jackson.core.type.TypeReference;

import java.io.Serializable;

public class DefaultMethods implements Serializable {
    private static final long serialVersionUID = GlobalEnv.SerialVersionUID;

    @Override
    public String toString() {
        return getClass().getSimpleName() + " " + JSONUtils.toJSON(this, Feature.PRETTY_PRINT, Feature.SUPPORT_GSON_ANNOTATION);
    }

    public JSONObject toJSONObject() {
        return JSONUtils.toObj(this, new TypeReference<JSONObject>() {
        });
    }

    public String toJSON() {
        return JSONUtils.toJSON(this);
    }

    @SuppressWarnings("unchecked")
    public <T> T copy() {
        return (T) JSONUtils.deepClone(this);
    }

    public boolean equals(Object obj) {
        if (this == obj) return true;
        if (obj == null || getClass() != obj.getClass()) return false;
        return JSONUtils.from(this).equals(JSONUtils.from(obj));
    }
}
